package com.cn.xly2023.service;

import com.cn.xly2023.mapper.UserMapper;
import com.cn.xly2023.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    public Map<String,Object> selectById(Integer id){
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功!");
        map.put("data",userMapper.selectById(id));


        return map;
    }

    public Map<String,Object> selectAll(){
        List<User> userList = userMapper.selectAll();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","登陆成功");
        map.put("count",userList.size());
        map.put("data",userList);
        return map;
    }



    public Map<String,Object> selectByPage( Integer page, Integer limit,String username,Integer age){
        Integer index = (page - 1) *limit;
        List<User> userList = userMapper.selectByPage(index,limit,username,age);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","登陆成功");
        map.put("count",userMapper.selectCount(username,age));
        map.put("data",userList);
        return map;
    }



    public Map<String,Object> insert(User user){
        user.setCreateTime(new Timestamp(System.currentTimeMillis()));//获取当前时间并且给createTime赋值
        user.setPassword(md5(user.getUsername(), user.getPassword())); //把得到的密码使用MD5加密一次
        userMapper.insert(user);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","插入成功");
        map.put("data",user);
        return map;
    }



    public Map<String,Object> updateById(Integer id,Integer age){
        userMapper.updateById(id, age);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","更新成功");
        return map;
    }

    public Map<String,Object> deleteById(Integer id){
        userMapper.deleteById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","删除成功");
        return map;
    }

    //登录功能
    public Map<String,Object> login(String username,String password){

        Map<String,Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isAuthenticated()){
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);
            upToken.setRememberMe(true);
            try{
                subject.login(upToken);
                map.put("code",200);
                map.put("msg","登陆成功!");
                return map;

            }
            catch (IncorrectCredentialsException ice){
                System.out.println("------------用户名/密码错误-------------");
                map.put("code",5001);
                map.put("msg","您输入的用户名或密码错误!!!");
                return map;

            }
            catch (LockedAccountException lae){
                System.out.println("------------账户已被锁定----------------");
                map.put("code",5002);
                map.put("msg","您的账户已被锁定!!!");
                return map;
            }

            catch (UnknownAccountException uae){
                System.out.println("------------未知账户----------------");
                map.put("code",5003);
                map.put("msg","未知的账户!!!");
                return map;
            }
            catch (AuthenticationException ae){
                System.out.println("--------------未知错误---------------");
                System.out.println(ae.getMessage());
                map.put("code",5004);
                map.put("msg","产生未知的错误!!!");
                return map;
            }
        }
        map.put("code",5005);
        map.put("msg","登陆异常,请稍后重试!!!");
        return map;
    }

    public User getUser(String username){
        return userMapper.login(username);
    };

    public String test01() {
        return "hello Springboot";
    }


    // 注册时，进行shiro加密，返回加密后的结果，如果在加入shiro之前，存在用户密码不是此方式加密的，那么将无法登录
    // 使用用户名作为盐值
    private static String md5(String username, String password){
        String hashAlgorithmName = "MD5";                   // 加密方式
        ByteSource salt = ByteSource.Util.bytes(username);  // 以账号作为盐值
        int hashIterations = 11;                            // 加密11次
        return new SimpleHash(hashAlgorithmName, password, salt, hashIterations).toString();
    }
}
